package com.gegao.somethinginteresting.utils.fresco;

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import com.facebook.common.executors.CallerThreadExecutor;
import com.facebook.datasource.BaseDataSubscriber;
import com.facebook.datasource.DataSource;
import com.facebook.datasource.DataSubscriber;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.gegao.somethinginteresting.utils.fresco.callback.FrescoClearCompleteCallback;
import com.gegao.somethinginteresting.utils.fresco.callback.FrescoUriIsInCacheCallback;


/**
 * Fresco缓存管理工具
 * Created by liuchenghui on 2016/01/05.
 */
public class FrescoCacheUtils {

    private static Handler handler = new Handler();

    /**
     * 清除缓存
     * imagePipeline.evictFromMemoryCache(uri);
     * + imagePipeline.evictFromDiskCache(uri);
     * =imagePipeline.evictFromCache(uri);
     *
     * @param uriStr uri
     */
    public static void clearUriCache(FRESCOURITYPE URITYPE, final String uriStr,String packageName ,final FrescoClearCompleteCallback clearCompletes) {
        final Uri uri = UriUtils.getUri(uriStr, URITYPE,packageName);
        final ImagePipeline imagePipeline = Fresco.getImagePipeline();
        isIndisk(uri, new FrescoUriIsInCacheCallback() {
            @Override
            public void isInDiskCache(boolean isIn) {
                if (imagePipeline.isInBitmapMemoryCache(uri))
                    imagePipeline.evictFromMemoryCache(uri);
                if (isIn)
                    imagePipeline.evictFromDiskCache(uri);
                //执行回调
                if (clearCompletes != null) {
                    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                clearCompletes.doSomething();
                            }
                        });
                    } else {
                        clearCompletes.doSomething();
                    }
                }
            }
        });
    }

    /**
     * uri是否在 磁盘 缓存中(异步的)
     *
     * @param uri      uri
     * @param callback 回调
     */
    public static void isIndisk(Uri uri, final FrescoUriIsInCacheCallback callback) {
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        DataSource<Boolean> inDiskCacheSource = imagePipeline.isInDiskCache(uri);
        DataSubscriber<Boolean> subscriber = new BaseDataSubscriber<Boolean>() {
            @Override
            protected void onNewResultImpl(final DataSource<Boolean> dataSource) {
                if (!dataSource.isFinished()) {
                    return;
                }
                callback.isInDiskCache(dataSource.getResult());
            }

            @Override
            protected void onFailureImpl(DataSource<Boolean> dataSource) {

            }
        };
        inDiskCacheSource.subscribe(subscriber, CallerThreadExecutor.getInstance());
    }
}
